/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import DTO.Address;
import DTO.Item;
import DTO.Order;
import DesktopGUIpackage.*;
import Exception.KwickKioskDBException;
import java.lang.Exception;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JApplet;
import javax.swing.JLabel;


/**
 *
 * @author tejinder
 */
public class DesktopControllerHelper extends JApplet{

    private DesktopController desktopController;
    private mainFrame frame;


    public void init(){
        

        try{
            desktopController = new DesktopController(this);
        }
        catch (KwickKioskDBException e){
            Desktop_ErrorDatabaseDown errorDatabaseDown = new Desktop_ErrorDatabaseDown(this, e.getShortMessage());
            errorDatabaseDown.setBounds(0, 0, 800, 600);
            add(errorDatabaseDown);
            setVisible(true);
            return;
        }
        frame = new mainFrame(this);
        frame.setSize(1042, 750);
        setSize(1042, 750);

        add(frame);
        changeFrame(1);
    }

    public void start(){
        
    }

    public DesktopController getDesktopController() {
        return desktopController;
    }


    public void changeFrame(int choice) {
        if (choice == 1) {//mainout only
            frame.mainOutScreen();
            SearchScreen();
        } else if (choice == 2) {//main in after log in
            frame.mainInScreen();
            SearchScreen();
        } else if (choice == 3) {//register confirmed after register
            frame.mainOutScreen();
            RegisterConfirmScreen();

        } else if (choice == 4) {//password recovewry
            frame.mainOutScreen();
            PasswordRecoveryScreen();

        } else if (choice == 5) {// confirm when loggout
            frame.mainOutScreen();
            ConfirmScreen();
        } else if (choice == 6) {//databsae down erors
             frame.databaseDownScreen();
        } else if (choice == 7) {//adding address screen
            frame.mainOutScreen();
            addAddressScreen();
        } else if (choice == 8) {// confirm when loggin
            frame.mainInScreen();
            if (desktopController.getSessionMgr().isOnPathToPurchase()) {
                cartScreen();
            } else {
                ConfirmScreen();
            }
        } else if (choice == 9) {//order history
            frame.mainInScreen();
            OrderHistoryScreen();
        } else if (choice == 10) {//if try to check out without login
            //frame.mainInScreen();
            cartScreen();
        } else if (choice == 11) {//if try to check out without login
            frame.mainInScreen();
            cartScreen();
        } else if (choice == 12){ //
            frame.databaseDownScreen();
            
        }

    }

    public void refresh(){
        frame.setTotals();
        frame.repaint();
    }

    public void LogInScreen() {
        Desktop_LogIn login = new Desktop_LogIn(this);
        login.setBounds(0, 0, 800, 600);
        frame.changeScreen(login);
        frame.validate();
        frame.repaint();
    }

    public void MainScreen() {
        Desktop_MainOut mainScreen = new Desktop_MainOut(this);
        mainScreen.setBounds(0, 0, 800, 600);
        frame.removeAll();
        frame.add(mainScreen);
        frame.repaint();
        changeFrame(1);
    }

    public void RegisterationScreen() {

        Desktop_AddUser regScreen = new Desktop_AddUser(this);
        regScreen.setBounds(0, 0, 800, 600);
        frame.changeScreen(regScreen);
        frame.validate();
        frame.repaint();
    }

    public void MainInScreen() {
        Desktop_MainIn mainInScreen = new Desktop_MainIn(this);
        mainInScreen.setBounds(0, 0, 800, 600);
        frame.removeAll();
        frame.add(mainInScreen);
        frame.validate();
        frame.repaint();
    }

    public void addAddressScreen() {
        Desktop_AddAddress regAddress = new Desktop_AddAddress(this);
        regAddress.setBounds(0, 0, 800, 600);
        frame.changeScreen(regAddress);
        frame.validate();
        frame.repaint();
    }

     public void editAddressScreen(Address address) {
        Desktop_EditAddress regAddress = new Desktop_EditAddress(this, address );
        regAddress.setBounds(0, 0, 800, 600);
        frame.changeScreen(regAddress);
        frame.validate();
        frame.repaint();
    }


    public void ManageAddressScreen() {
        Desktop_ManageAddresses delAddress = new Desktop_ManageAddresses(this);
        delAddress.setBounds(0, 0, 800, 600);
        frame.changeScreen(delAddress);
        frame.validate();
        frame.repaint();
    }

    public void SearchScreen() {
        Desktop_SearchItem search = new Desktop_SearchItem(this);
        search.setBounds(0, 0, 800, 600);
        frame.changeScreen(search);
        frame.validate();
        frame.repaint();
    }

    public void RegisterConfirmScreen() {
        Desktop_RegisterationConfirm registerationConfirm = new Desktop_RegisterationConfirm(this);
        registerationConfirm.setBounds(0, 0, 800, 600);
        frame.changeScreen(registerationConfirm);
        frame.validate();
        frame.repaint();
    }

    public void OrderHistoryScreen() {
        ArrayList<Order> list = new ArrayList<Order>();
        int csid = desktopController.getSessionMgr().getCutomerId();
        System.out.println("customer id for order history " + csid);
        list = desktopController.getOrderHistory(csid);
        Desktop_ViewOrders viewOrders = new Desktop_ViewOrders(this, list);
        viewOrders.setBounds(0, 0, 800, 600);
        frame.changeScreen(viewOrders);
        frame.validate();
        frame.repaint();
    }

    public void ItemHistoryScreen(Order order) {

        ArrayList<Item> itemList;
        itemList = (ArrayList<Item>) desktopController.getItemHistory(order.getID());

        Desktop_OrderHistory viewOrders = new Desktop_OrderHistory(this, itemList);
        viewOrders.setBounds(0, 0, 800, 600);
        frame.changeScreen(viewOrders);
        frame.validate();
        frame.repaint();
    }

    public void editUserScreen() {
        Desktop_EditUser editUser = new Desktop_EditUser(this);
        editUser.setBounds(0, 0, 800, 600);
        frame.changeScreen(editUser);
        frame.validate();
        frame.repaint();
    }

    public void PasswordRecoveryScreen() {
        Desktop_RecoverPassword recoverPassword = new Desktop_RecoverPassword(this);
        recoverPassword.setBounds(0, 0, 800, 600);
        frame.changeScreen(recoverPassword);
        frame.validate();
        frame.repaint();
    }

    public void ConfirmScreen() {
        Desktop_ConfirmMessage recoverPasswordcConfirm = new Desktop_ConfirmMessage(this, desktopController.getMessage());
        recoverPasswordcConfirm.setBounds(0, 0, 800, 600);
        frame.changeScreen(recoverPasswordcConfirm);
        frame.validate();
        frame.repaint();
    }

    public void ErrorScreen() {
        Desktop_ErrorDatabaseDown errorDatabaseDown = new Desktop_ErrorDatabaseDown(this, "");
        errorDatabaseDown.setBounds(0, 0, 800, 600);
        frame.changeScreen(errorDatabaseDown);
        frame.validate();
        frame.repaint();
    }

    public void ItemDetailsScreen(Item item) {
        Desktop_ItemDetails itemDetails = new Desktop_ItemDetails(this, item);
        itemDetails.setBounds(0, 0, 800, 600);
        frame.changeScreen(itemDetails);
        frame.validate();
        frame.repaint();
    }

    public void cartScreen() {
        refresh();
        Desktop_ViewCart viewcart = new Desktop_ViewCart(this);
        viewcart.setBounds(0, 0, 800, 600);
        frame.changeScreen(viewcart);
        frame.validate();
        frame.repaint();
    }

    public void paymentScreen() {
        Desktop_Payment pymtscreen = new Desktop_Payment(this);
        pymtscreen.setBounds(0, 0, 800, 600);
        frame.changeScreen(pymtscreen);
        frame.validate();
        frame.repaint();


    }

    public void orderConfirmScreen() {
        Desktop_PayOK ordercnfrm = new Desktop_PayOK(this);
        ordercnfrm.setBounds(0, 0, 800, 600);
        frame.changeScreen(ordercnfrm);
        frame.validate();
        frame.repaint();


    }

    public void orderFailedScreen() {
        Desktop_PayError payError = new Desktop_PayError(this);
        payError.setBounds(0, 0, 800, 600);
        frame.changeScreen(payError);
        frame.validate();
        frame.repaint();
    }

    public void DatabaseDownScreen() {
        changeFrame(12);
    }
}
